﻿using System;
using System.Dynamic;
using System.Linq.Expressions;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UPNP.IO;

namespace UPNP.Test
{
    [TestClass]
    public class DynamicObjectTest
    {
        public const int TestOutParamValue = -837645782;

        public class TestObject : DynamicObject
        {
            public override IEnumerable<string> GetDynamicMemberNames()
            {
                return new[]
                {
                    "TestOutParam"
                };
            }

            public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
            {
                result = null;

                switch (binder.Name)
                {
                    case "TestOutParam":
                        Assert.AreEqual(1, args.Length);
                        Assert.AreEqual(1, binder.CallInfo.ArgumentCount);

                        args[0] = TestOutParamValue;

                        break;

                    case "TestOutParamValue":
                        Assert.AreEqual(0, args.Length);
                        Assert.AreEqual(0, binder.CallInfo.ArgumentCount);

                        result = new DynamicReturn()
                            {
                                { "TestOutParamValue", TestOutParamValue }
                            };

                        break;

                    default:
                        return false;
                }

                return true;
            }
        }

        [TestMethod]
        public void TestOutParam()
        {
            dynamic foo = new TestObject();

            int o;

            foo.TestOutParam(out o);

            Assert.AreEqual(/*TestOutParamValue*/0, o);
        }

        [TestMethod]
        public void TestDynamicReturn()
        {
            dynamic foo = new TestObject();

            var r = foo.TestDynamicReturn();

            Assert.AreEqual(TestOutParamValue, r.TestOutParamValue);
        }
    }
}
